package com.song.config;

import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;

import java.util.Arrays;
import java.util.List;

@Component
public class SwaggerConfig implements SwaggerResourcesProvider {
    @Override
    public List<SwaggerResource> get() {
        return Arrays.asList(
//                createResource("用户服务","1.0","/user/v2/api-docs"),
//                createResource("产品服务","1.0","/product-server/v2/api-docs"),
                createResource("用户服务","1.0","/us/v2/api-docs")
                ,createResource("分析服务","1.0","/al/v2/api-docs")
//                , createResource("认知服务","1.0","/oc/v2/api-docs")
        );
    }

//    /**
//     * swagger2默认的url后缀
//     */
//    private static final String SWAGGER2URL = "/v2/api-docs";
//
//    /**
//     * 网关路由
//     */
//    private final RouteLocator routeLocator;
//
//    /**
//     * 网关应用名称
//     */
//    @Value("${spring.application.name}")
//    private String self;
//
//    @Autowired
//    public SwaggerConfig(RouteLocator routeLocator) {
//        this.routeLocator = routeLocator;
//    }
//
//    @Override
//    public List<SwaggerResource> get() {
//        List<SwaggerResource> resources = new ArrayList<>();
//        List<String> routeHosts = new ArrayList<>();
//        // 获取所有可用的host：serviceId
//        routeLocator.getRoutes().filter(route -> route.getUri().getHost() != null)
//                .filter(route -> !self.equals(route.getUri().getHost()))
//                .subscribe(route -> routeHosts.add(route.getUri().getHost()));
//
//        // 记录已经添加过的server，存在同一个应用注册了多个服务在eureka上
//        Set<String> dealed = new HashSet<>();
//        routeHosts.forEach(instance -> {
//            // 拼接url
//            String url = "/ps/" + instance.toLowerCase() + SWAGGER2URL;
//            if (!dealed.contains(url)) {
//                dealed.add(url);
//                SwaggerResource swaggerResource = new SwaggerResource();
//                swaggerResource.setUrl(url);
//                swaggerResource.setName(instance);
//                resources.add(swaggerResource);
//            }
//        });
//        return resources;
//    }

    private SwaggerResource createResource(String name,String version,String addr) {
        SwaggerResource sr = new SwaggerResource();
        sr.setName(name);
        sr.setSwaggerVersion(version);
        sr.setLocation(addr);
        return sr;
    }
}
